package com.example.brvah_test;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.android.iplayer.controller.VideoController;
import com.android.iplayer.widget.VideoPlayer;
import com.android.iplayer.widget.WidgetFactory;

import java.util.ArrayList;

public class VideoListActivity extends AppCompatActivity {

    private RecyclerView videolist_rv;
    private ArrayList<VideoBean> videoBeans;
    private VideoAdapter videoAdapter;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);
        videolist_rv = findViewById(R.id.videolist_rv);
        videolist_rv.setLayoutManager(new LinearLayoutManager(this));
        videoBeans = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            videoBeans.add(new VideoBean("http://vfx.mtime.cn/Video/2018/06/27/mp4/180627094726195356.mp4", "video" + i));
        }
        videoAdapter = new VideoAdapter(R.layout.item_video, videoBeans);
        videolist_rv.setAdapter(videoAdapter);
        videolist_rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            private int thisPosition = -1;

            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    if (layoutManager != null) {
                        int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
                        int eddVisibleItem = layoutManager.findFirstCompletelyVisibleItemPosition();
                        if (eddVisibleItem == thisPosition) return;
                        thisPosition = eddVisibleItem;//集合中的数据位置
                        int thisVideoView = eddVisibleItem - firstVisibleItem;//界面中ViewHolder中的位置
                        Log.i("xd===","firstVisibleItem==" +firstVisibleItem);
                        Log.i("xd===","eddVisibleItem==" +eddVisibleItem);
                        Log.i("xd===","thisPosition==" +thisPosition);
                        Log.i("xd===","thisVideoView==" +thisVideoView);
                        if (layoutManager.getChildAt(thisVideoView) != null
                                && layoutManager.getChildAt(thisVideoView).findViewById(R.id.item_video_vv) != null) {
                            com.android.iplayer.widget.VideoPlayer videoView = layoutManager.getChildAt(thisVideoView).findViewById(R.id.item_video_vv);
                           startVideo(videoView,videoBeans.get(thisPosition));
                        }
//                        videoAdapter.notifyDataSetChanged();
                    }
                } else if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {//手指拖动

                } else if (newState == RecyclerView.SCROLL_STATE_SETTLING) {//惯性滚动

                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);


            }
        });

    }

    private void startVideo(VideoPlayer videoView, VideoBean videoBean) {
        //使用SDK自带控制器+各UI交互组件
        VideoController controller = videoView.initController();//初始化SDK默认控制器
        //将UI交互组件绑定到控制器(需集成：implementation 'com.github.hty527.iPlayer:widget:lastversion')
        WidgetFactory.bindDefaultControls(controller);
        controller.setTitle("测试地址播放");//视频标题(仅横屏状态可见)
        //设置播放源
        videoView.setDataSource(videoBean.getUrl());
        //异步开始准备播放
        videoView.prepareAsync();
    }

}